<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true">
        <!-- <el-form-item>
          <el-select v-model="listQuery.dependCode" class="w300"
            filterable collapse-tags clearable @change="selectChange"
            placeholder="请选择院士大师分类">
            <el-option v-for="item in collegeOptions"
              :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-input placeholder="请输入要查找的人物姓名或头衔" clearable
            v-model="listQuery.key" class="w300"></el-input>
        </el-form-item>
        <el-button type="primary" @click="fetchList"
          :loading="listLoading" style="width: 100px;">
          查询
        </el-button>
        <el-button type="success" style="width: 150px;"
          @click="drawer = true;formData = {}">新增人物</el-button>
      </el-form>

      <el-table style="min-height: 200px;"
        v-loading="listLoading"
        :data="list"
        element-loading-text="数据加载中"
        border
        stripe
        fit
        highlight-current-row
        class="table"
        ref="multipleTable"
      >
          <el-table-column label="头像" align="center" width="120" prop="code">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="点击预览" placement="right">
                <el-image :src="scope.row.img" :preview-src-list="[scope.row.img]"
                  style="width: 30px;height: 30px;" />
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="名字" align="center" prop="name" min-width="150"/>
          <el-table-column label="头衔" align="center" min-width="200" prop="title"/>
          <!-- <el-table-column label="所属分类" align="center" min-width="180" prop="dependDescr"/> -->
          <el-table-column label="新增时间" align="center" width="160" prop="createdTime" />
          <el-table-column label="操作" align="right"
            class-name="small-padding fixed-width" width="250">
            <template slot-scope="scope">
              <el-button-group>
                <el-button size="mini" type="info"
                  @click="drawer3 = true;detailData = scope.row">人物预览</el-button>
                <el-button size="mini" type="warning"
                  @click="update(scope.row)">修改</el-button>
                <el-popconfirm title="确定删除该文章？"
                  @confirm="deleteById(scope.row.id)">
                  <el-button slot="reference" size="mini" type="danger">删除</el-button>
                </el-popconfirm>

              </el-button-group>
            </template>
          </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: center;"
        v-show="total>0"
        :total="total"
        :current-page.sync="listQuery.page"
        :page-size.sync="listQuery.size"
        layout="sizes,prev,slot,next,jumper"
        prev-text="上一页"
        next-text="下一页"
        @size-change="fetchList"
        @current-change="fetchList"
      >
        <span>{{listQuery.page}}/{{ Math.trunc((total+ listQuery.size-1) / listQuery.size)}}</span>
      </el-pagination>
    </div>

    <el-drawer
      title="人物基本信息"
      size="40%"
      :visible.sync="drawer"
      direction="rtl"
      :destroy-on-close="true"
      :before-close="handleClose">
      <el-form inline style="padding: 0 40px 120px 25px;"
        class="overflowAuto"
        :model="formData" :rules="rules" ref="saveForm" label-width="120px" >
         <el-form-item required label="头像" prop="img" :show-message="false">
           <el-upload
             :on-success="imgUpload"
             :file-list="formData.imgList"
             :action="baseURL + '/cos/upload?type=article_img'"
             :headers="{authorization: 'Bearer ' + token()}"
             :on-remove="()=>{formData.img = ''}"
             :accept="imgTypeList.join(',')"
             :before-upload="imgCheck"
             name="file"
             list-type="picture-card"
             :limit="1">
             <i class="el-icon-plus"></i>
             <div slot="tip" class="el-upload__tip">
               只能上传.jpg/.png，且不超过2M
             </div>
           </el-upload>
           <div class="el-form-item__error">{{imgError}}</div>
         </el-form-item>
         <!-- <el-form-item required label="院士大师分类" prop="depend">
           <el-select v-model="formData.depend" class="w300"
             filterable collapse-tags clearable value-key="code"
             placeholder="请选择院士大师分类">
             <el-option v-for="item in collegeOptions"
               :key="item.code" :label="item.name" :value="item"/>
           </el-select>
         </el-form-item> -->
         <el-form-item label="姓名" prop="name">
            <el-input placeholder="请输入姓名" v-model="formData.name" class="w300"></el-input>
         </el-form-item>
         <el-form-item label="主要头衔" prop="title">
            <el-input placeholder="请输入主要头衔" v-model="formData.title" class="w300"></el-input>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" style="margin-left: 30px;"
              @click="openEditor">
              保存，开始编辑人物介绍内容
            </el-button>
         </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer
      size="100%"
      :visible.sync="editDrawer"
      direction="rtl"
      :destroy-on-close="true"
      :before-close="handleClose">
      <template slot="title">
        <div>
          <el-button type="primary" @click="save"
            style="margin-right: 30px;width: 100px;"
            :loading="subing">
            保存
          </el-button>
          <span>{{formData.name}}（{{formData.title}}）</span>
        </div>
      </template>
      <div style="padding: 0 40px 120px 25px;" class="overflowAuto">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 500px; overflow-y: hidden;"
            v-model="formData.info"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
      </div>
    </el-drawer>

    <el-drawer
      custom-class="detail-drawer"
      title=""
      size="100%"
      @opened="opened"
      :visible.sync="drawer3"
      direction="rtl">
      <template slot="title">
        <div>
          <span>人物预览 - {{detailData.name}}（{{detailData.title}}）</span>
        </div>
      </template>
      <p style="text-align: center;">
        <img style="width: 200px;":src="detailData.img"/>
      </p>
      <div id="articleView"
        style="width: 800px;margin-left: auto;margin-right: auto;"></div>
    </el-drawer>
  </div>
</template>

<script>
import person from '@/mixins/person.js'
import v from '@/utils/formValidate'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
  components: { Editor, Toolbar },
  mixins: [person],
  props: ['layout'],
  data() {
    return {
      rules: {
        // depend: [
        //   { required: true, message: '请选择院士大师分类', trigger: 'change' },
        // ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'change' },
          { validator: v.validate({ blank: true }), trigger: 'change' },
          { max: 8, message: '长度不超过 8 个字符', trigger: 'change' },
        ],
        title: [
          { required: true, message: '请输入主要头衔', trigger: 'change' },
          { validator: v.validate({ blank: true }), trigger: 'change' },
          { max: 16, message: '长度不超过 16 个字符', trigger: 'change' },
        ],


      },
    }
  },
  methods: {
  }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped >
  @import "~@/styles/init.scss";
</style>
<style>
  .detail-drawer {
    padding-bottom: 10px;
  }
  .el-table thead {
    color: #303133 !important;
  }
</style>
